package services.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import models.common.bean.ShowCompanyInfo;
import models.common.entity.t_reddish_black;
import services.base.BaseService;

import common.constants.Constants;
import common.enums.MemberType;
import common.utils.Factory;
import common.utils.PageBean;

import daos.common.ReddishBlackDao;

/**
 * 企业红黑名单业务层
 *@author liangqiongfeng
 *@version 2018-5-9 下午8:37:34
 */
public class ReddishBlackService extends BaseService<t_reddish_black>{

	protected static ReddishBlackDao reddishBlackDao = Factory.getDao(ReddishBlackDao.class); 
	
	protected ReddishBlackService() {
		super.basedao = this.reddishBlackDao;
	}

	/**
	 * 保存红黑名单企业
	 * @param reddish_black
	 * @return
	 * @author liangqiongfeng
	 *@version 2018-5-9 
	 */
	public boolean save(t_reddish_black reddish_black) {
		return reddishBlackDao.save(reddish_black);
	}

	/**
	 *	注册时校验企业红黑名单
	 * @param name
	 * @return
	 * @author liangqiongfeng
	 *@version 2018-5-10 
	 */
	public t_reddish_black findByCompanyName(String name) {
		StringBuffer querySQL = new StringBuffer("SELECT id,company_full_name,time,type FROM `t_reddish_black` WHERE company_full_name = :company_full_name ");
		Map<String,Object> condition = new HashMap<String, Object>();
		condition.put("company_full_name", name);
		return reddishBlackDao.findBySQL(querySQL.toString(), condition);
	}

	/**
	 *
	 * @param fullName 企业全称
	 * @param exports 是否导出
	 * @param orderValue 排序
	 * @param type 状态 4 黑名单 ， 3 红名单
	 * @param currPage 
	 * @param pageSize 
	 * @return
	 * @author liangqiongfeng
	 *@version 2018-5-10 
	 */
	public PageBean<t_reddish_black> findReddishOfPage(String fullName,
			int exports, int currPage, int pageSize, int orderValue, int type) {
		StringBuffer countSQL = new StringBuffer("SELECT COUNT(1) FROM `t_reddish_black` WHERE 1 = 1 ");
		StringBuffer querySQL = new StringBuffer("SELECT id,company_full_name,time,type FROM `t_reddish_black` WHERE  1 = 1 ");
		
		Map<String, Object> condition = new HashMap<String, Object>();
		
		if (StringUtils.isNotBlank(fullName)) {
			/* 按企业全称搜索 */
			countSQL.append(" AND company_full_name like :fullName");
			querySQL.append(" AND company_full_name like :fullName");
			condition.put("fullName", "%"+fullName+"%");
		}
		
		if (type >= 0) {
			/* 按企业全称搜索 */
			countSQL.append(" AND type =:type");
			querySQL.append(" AND type =:type");
			condition.put("type", type);
		}
		querySQL.append(" ORDER BY id ");
		if(orderValue == 0){
			querySQL.append(" DESC ");
		}
		
		if(exports == Constants.EXPORT){
			PageBean<t_reddish_black> page = new PageBean<t_reddish_black>();
			page.page = reddishBlackDao.findListBeanBySQL(querySQL.toString(),t_reddish_black.class, condition);
			
			return page;
		}
		return reddishBlackDao.pageOfBeanBySQL(currPage, pageSize, countSQL.toString(), querySQL.toString(),t_reddish_black.class, condition);
	}
	
	
}
